package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.model.behavior.dtos.IsFollowDto;
import com.heima.model.behavior.pojos.ApArticleUnlike;
import com.heima.model.user.dtos.UserFollowDto;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.UserFollowService;
import com.heima.utils.common.WmThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@RequiredArgsConstructor
@Service
@Slf4j
public class UserFollowServiceImpl implements UserFollowService {

    private final ApUserFanMapper apUserFanMapper;
    private final ApUserFollowMapper apUserFollowMapper;

    @Override
    @Transactional
    public void follow(UserFollowDto userFollowDto) {
        // 当前登录的用户 id
        Long userId = Long.valueOf(WmThreadLocalUtil.getUser().getId());
        // 当前登录的用户姓名 name
        String name = WmThreadLocalUtil.getUser().getName();

        LambdaQueryWrapper<ApUserFan> apUserFanWrapper = new LambdaQueryWrapper<>();
        apUserFanWrapper.eq(ApUserFan::getUserId, userFollowDto.getAuthorId())  // 作者 id 相同
                .eq(ApUserFan::getFansId, userId);  // 粉丝 id 与 当前登录用户 id 相同

        LambdaQueryWrapper<ApUserFollow> apUserFollowWrapper = new LambdaQueryWrapper<>();
        apUserFollowWrapper.eq(ApUserFollow::getUserId, userId)  // 当前登录用户id 相同
                .eq(ApUserFollow::getFollowId, userFollowDto.getAuthorId());  // 关注者 与 当前作者 id 相同


        // 获取操作类型, 如果是取消关注则需要删除原有数据, 否则需要插入新数据
        Short operation = userFollowDto.getOperation();

        /* 取消关注的情况 */
        if (operation == 1) {
            /* 删除 ap_user_fan 表中的数据  */
            apUserFanMapper.delete(apUserFanWrapper);
            /* 删除 ap_user_follow 表中的数据 */
            apUserFollowMapper.delete(apUserFollowWrapper);
            return;
        }

        /* if (operation == 0) 如果是关注操作 */
        /* 保证操作的幂等性(保证在插入的时候, 不会出现重复的数据) */
        ApUserFan apUserFanExist = apUserFanMapper.selectOne(apUserFanWrapper);
        ApUserFollow apUserFollowExit = apUserFollowMapper.selectOne(apUserFollowWrapper);
        if (apUserFanExist != null && apUserFollowExit != null) {  // 表示数据库中已经存在关注信息, 再次关注属于非法操作
            return;
        }

        /* ap_user_fan 中添加数据 */
        ApUserFan apUserFan = new ApUserFan();
        apUserFan.setUserId(userFollowDto.getAuthorId());
        apUserFan.setFansId(userId);
        apUserFan.setFansName(name);
        apUserFan.setLevel(ApUserFan.FansLevel.NORMAL.getLevelValue());
        apUserFan.setCreatedTime(LocalDateTime.now());
        apUserFan.setDisplayed((short) 0);
        apUserFan.setShieldLettered((short) 0);
        apUserFan.setShieldCommented((short) 0);
        apUserFanMapper.insert(apUserFan);

        /* ap_user_follow 中添加数据 */
        ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(userId);
        apUserFollow.setFollowId(userFollowDto.getAuthorId());
        apUserFollow.setFollowName(name);
        apUserFollow.setLevel(ApUserFollow.CareLevel.SELDOM.getCareLevel());
        apUserFollow.setNoticed((short) 0);
        apUserFollow.setCreatedTime(LocalDateTime.now());
        apUserFollowMapper.insert(apUserFollow);

    }

    @Override
    public Boolean isFollow(IsFollowDto isFollowDto) {
        LambdaQueryWrapper<ApUserFollow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserFollow::getUserId, isFollowDto.getUserId())
                .eq(ApUserFollow::getFollowId, isFollowDto.getAuthorId());

        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(wrapper);
        return apUserFollow != null;
    }
}
